package cn.e4j.modular.system.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.model.exception.ServiceException;
import cn.e4j.modular.system.entity.SysRole;
import cn.e4j.modular.system.entity.SysRoleCmp;
import cn.e4j.modular.system.entity.SysRoleModule;
import cn.e4j.modular.system.entity.SysUserRole;
import cn.e4j.modular.system.mapper.SysRoleMapper;
import cn.e4j.modular.system.service.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 *
 * @author yuan_rz
 * @Date 2019/01/04 10:21
 */
@Controller
@RequestMapping("/sys/role")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysRoleModuleService sysRoleModuleService;

    @Autowired
    private SysRoleCmpService sysRoleCmpService;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 跳转用户管理页面
     */
    @RequiresPermissions("sys:role:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/system/role.html";
    }

    /**
     * 查询角色列表
     */
    @RequestMapping("/getRolesList")
    @RequiresPermissions("sys:role:init")
    @ResponseBody
    public ResponseData getRolesList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
//        inDto.put("is_enable", "1");
        List<Map<String, Object>> result = sysRoleService.getRolesList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询角色 for 用户管理
     */
    @RequestMapping("/getRoles")
    @RequiresPermissions("user")
    @ResponseBody
    public ResponseData getRoles(Map<String,String> map) {
        List<Map<String, Object>> result = sysRoleService.getRoles();
        return new SuccessResponseData(result);
    }

    @RequestMapping(value = "/exportRoles", method = RequestMethod.GET)
    @RequiresPermissions("sys:role:init")
    public void exportRoles(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
        List<SysRole> list = sysRoleMapper.getRoles(null);
        ExportParams params = new ExportParams(null, "角色数据", ExcelType.XSSF);
        //params.setFreezeCol(2);
        map.put(NormalExcelConstants.FILE_NAME, "角色数据");
        map.put(NormalExcelConstants.DATA_LIST, list);
        map.put(NormalExcelConstants.CLASS, SysRole.class);
        map.put(NormalExcelConstants.PARAMS, params);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

    /**
     * 添加
     */
    @RequestMapping("/add")
    @RequiresPermissions("sys:role:init")
    @ResponseBody
    public ResponseData add(SysRole sysRole, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        sysRoleService.add(sysRole);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:role:init")
    @ResponseBody
    public ResponseData del(SysRole sysRole, BindingResult result) {
        sysRoleService.delete(sysRole);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("sys:role:init")
    @ResponseBody
    public ResponseData edit(SysRole sysRole, BindingResult result) {
        sysRoleService.edit(sysRole);
        return SUCCESS_TIP;
    }

    /**
     * 查询角色列表
     */
    @RequestMapping("/getEnableRoles")
    @RequiresPermissions("sys:role:init")
    @ResponseBody
    public ResponseData getEnableRoles(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        inDto.put("is_del", "0");
        return new SuccessResponseData(sysRoleService.listRolesExport(inDto));
    }

    /**
     * 查询用户列表
     */
    @RequestMapping("/getUsersList")
    @RequiresPermissions("sys:role:init")
    @ResponseBody
    public ResponseData getUsersList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = Dtos.newDto();
        inDto.put("is_del", "0");
        return new SuccessResponseData(sysUserService.getUserslist(inDto));
    }

    /**
     * 查询已选用户列表
     */
    @RequestMapping("/getRoleUsersList")
    @RequiresPermissions("sys:role:init")
    @ResponseBody
    public ResponseData getRoleUsersList(@RequestParam String roleId) {
        return new SuccessResponseData(sysUserRoleService.selectUseridsByRole(roleId));
    }

    /**
     * 给用户添加角色
     */
    @RequestMapping("/addRoleUsers")
    @RequiresPermissions("sys:role:init")
    @ResponseBody
    public ResponseData addRoleUsers(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<SysUserRole> list = inDto.getReqList(SysUserRole.class);
        sysUserRoleService.addUserRoles(list);
        return SUCCESS_TIP;
    }

    /**
     * 删除用户角色
     */
    @RequestMapping("/deleteRoleUsers")
    @RequiresPermissions("sys:role:init")
    @ResponseBody
    public ResponseData deleteRoleUsers(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<SysUserRole> list = inDto.getReqList(SysUserRole.class);
        sysUserRoleService.delUserRoles(list);
        return SUCCESS_TIP;
    }

    /**
     * 获取角色的模块授权信息
     */
    @RequestMapping("/getRoleModulesList")
    @RequiresPermissions("sys:role:init")
    @ResponseBody
    public ResponseData getRoleModulesList(@RequestParam String roleId) {
        return new SuccessResponseData(sysRoleModuleService.getRoleModulesList(roleId));
    }

    /**
     * 保存角色的模块授权信息
     */
    @RequestMapping("/saveRoleModulesList")
    @RequiresPermissions("sys:role:init")
    @ResponseBody
    public ResponseData saveRoleModulesList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<SysRoleModule> list = inDto.getReqList(SysRoleModule.class);
        sysRoleModuleService.saveRoleModulesList(list);
        return SUCCESS_TIP;
    }

    /**
     * 获取角色的组件授权信息
     */
    @RequestMapping("/getRoleCmpsList")
    @RequiresPermissions("sys:role:init")
    @ResponseBody
    public ResponseData getRoleCmpsList(@RequestParam String roleId, @RequestParam String moduleId) {
        return new SuccessResponseData(sysRoleCmpService.getRoleCmpsList(roleId, moduleId));
    }

    /**
     * 初始化角色的组件授权信息
     */
    @RequestMapping("/initRoleCmpsStatus")
    @RequiresPermissions("sys:role:init")
    @ResponseBody
    public ResponseData initRoleCmpsStatus(@RequestParam String roleId, @RequestParam String moduleId) {
        return new SuccessResponseData(sysRoleCmpService.initRoleCmps(roleId, moduleId));
    }

    /**
     * 修改角色的组件授权信息状态
     */
    @RequestMapping("/changeRoleCmpsStatus")
    @RequiresPermissions("sys:role:init")
    @ResponseBody
    public ResponseData changeRoleCmpsStatus(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<SysRoleCmp> list = inDto.getReqList(SysRoleCmp.class);
        sysRoleCmpService.changeRoleCmpsStatus(list);
        return SUCCESS_TIP;
    }

}
